package com.exam.controller;

import com.exam.entity.*;
import com.exam.serviceimpl.LoginServiceImpl;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.Map;


@RestController
public class LoginController {
    @Autowired
    private LoginServiceImpl loginService;
    static int ad=0;
    static int te=0;
    static int st=0;
    @PostMapping("/login")
    public ApiResult login(@RequestBody Login login) {
        int j=ad;
        int k=te;
        int l=st;

        Jedis jedis = new Jedis("localhost", 6379);

        Integer username = login.getUsername();
        String password = login.getPassword();
        Admin adminRes=new Admin();
        int m1,m2,m3;
        for(m1=0;m1<=j;m1++)
        {
            if(jedis.hgetAll("admin"+m1).get("adminId")!=null)
            {
                boolean a=jedis.hgetAll("admin"+m1).get("adminId").equals(String.valueOf(username));
                boolean b=jedis.hgetAll("admin"+m1).get("pwd").equals(password);
                if(a&&b)
                {
                    Map<String,String> map=jedis.hgetAll("admin"+m1);
                    adminRes.setAdminId(Integer.valueOf(map.get("adminId")) );
                    adminRes.setAdminName(map.get("adminName"));
                    adminRes.setSex(map.get("sex"));
                    adminRes.setTel(map.get("tel"));
                    adminRes.setEmail(map.get("email"));
                    adminRes.setPwd(map.get("pwd"));
                    adminRes.setCardId(map.get("cardId"));
                    adminRes.setRole(map.get("role"));
                    return ApiResultHandler.buildApiResult(200, "请求成功", adminRes);
                }
            }

        }
        if (m1==j+1)
        {
            adminRes = loginService.adminLogin(username, password);
            if(adminRes!=null)
            {
                jedis.hset("admin"+j,"adminId", String.valueOf(adminRes.getAdminId()));
                jedis.hset("admin"+j,"adminName",adminRes.getAdminName());
                jedis.hset("admin"+j,"sex",adminRes.getSex());
                jedis.hset("admin"+j,"tel",adminRes.getTel());
                jedis.hset("admin"+j,"email",adminRes.getEmail());
                jedis.hset("admin"+j,"pwd",password);
                jedis.hset("admin"+j,"cardId",adminRes.getCardId());
                jedis.hset("admin"+j,"role",adminRes.getRole());
                ad++;
                return ApiResultHandler.buildApiResult(200, "请求成功", adminRes);
            }
        }

        Teacher teacherRes=new Teacher();
        for(m2=0;m2<=k;m2++)
        {
            if(jedis.hgetAll("teacher"+m2).get("teacherId")!=null)
            {
                boolean a=jedis.hgetAll("teacher"+m2).get("teacherId").equals( String.valueOf(username));
                boolean b=jedis.hgetAll("teacher"+m2).get("pwd").equals(password);
                if(a&&b)
                {
                    Map<String,String> map=jedis.hgetAll("teacher"+m2);
                    teacherRes.setTeacherId(Integer.valueOf(map.get("teacherId")));
                    teacherRes.setTeacherName(map.get("teacherName"));
                    teacherRes.setInstitute(map.get("institute"));
                    teacherRes.setSex(map.get("sex"));
                    teacherRes.setTel(map.get("tel"));
                    teacherRes.setEmail(map.get("email"));
                    teacherRes.setPwd(map.get("pwd"));
                    teacherRes.setCardId(map.get("cardId"));
                    teacherRes.setType(map.get("type"));
                    teacherRes.setRole(map.get("role"));
                    return ApiResultHandler.buildApiResult(200, "请求成功", teacherRes);
                }
            }

        }
        if (m2==k+1)
        {
            teacherRes = loginService.teacherLogin(username,password);
            if (teacherRes!=null)
            {
                jedis.hset("teacher"+k,"teacherId", String.valueOf(teacherRes.getTeacherId()));
                jedis.hset("teacher"+k,"teacherName",teacherRes.getTeacherName());
                jedis.hset("teacher"+k,"institute",teacherRes.getInstitute());
                jedis.hset("teacher"+k,"sex",teacherRes.getSex());
                jedis.hset("teacher"+k,"tel",teacherRes.getTel());
                jedis.hset("teacher"+k,"email",teacherRes.getEmail());
                jedis.hset("teacher"+k,"pwd",password);
                jedis.hset("teacher"+k,"cardId",teacherRes.getCardId());
                jedis.hset("teacher"+k,"type",teacherRes.getType());
                jedis.hset("teacher"+k,"role",teacherRes.getRole());
                te++;
                return ApiResultHandler.buildApiResult(200, "请求成功", teacherRes);
            }
        }
        Student studentRes=new Student();

        for(m3=0;m3<=l;m3++)
        {
            if(jedis.hgetAll("student"+m3).get("studentId")!=null)
            {
                boolean a=jedis.hgetAll("student"+m3).get("studentId").equals(String.valueOf(username));
                boolean b=jedis.hgetAll("student"+m3).get("pwd").equals(password);
                if(a&&b)
                {
                    Map<String,String> map=jedis.hgetAll("student"+m3);
                    studentRes.setStudentId(Integer.valueOf(map.get("studentId")));
                    studentRes.setStudentName(map.get("studentName"));
                    studentRes.setGrade(map.get("grade"));
                    studentRes.setMajor(map.get("major"));
                    studentRes.setClazz(map.get("clazz"));
                    studentRes.setInstitute(map.get("institute"));
                    studentRes.setTel(map.get("tel"));
                    studentRes.setEmail(map.get("email"));
                    studentRes.setPwd(map.get("pwd"));
                    studentRes.setCardId(map.get("cardId"));
                    studentRes.setSex(map.get("sex"));
                    studentRes.setRole(map.get("role"));
                    return ApiResultHandler.buildApiResult(200, "请求成功", studentRes);
                }
            }

        }
        if (m3==l+1)
        {
            studentRes = loginService.studentLogin(username,password);
            if (studentRes!=null)
            {
                jedis.hset("student"+l,"studentId", String.valueOf(studentRes.getStudentId()));
                jedis.hset("student"+l,"studentName",studentRes.getStudentName());
                jedis.hset("student"+l,"grade",studentRes.getGrade());
                jedis.hset("student"+l,"major",studentRes.getMajor());
                jedis.hset("student"+l,"clazz",studentRes.getClazz());
                jedis.hset("student"+l,"institute",studentRes.getInstitute());
                jedis.hset("student"+l,"tel",studentRes.getTel());
                jedis.hset("student"+l,"email",studentRes.getEmail());
                jedis.hset("student"+l,"pwd",password);
                jedis.hset("student"+l,"cardId",studentRes.getCardId());
                jedis.hset("student"+l,"sex",studentRes.getSex());
                jedis.hset("student"+l,"role",studentRes.getRole());
                st++;
                return ApiResultHandler.buildApiResult(200, "请求成功", studentRes);
            }
        }

        jedis.close();
        return ApiResultHandler.buildApiResult(400, "请求失败", null);
    }
}
